﻿
using System; 
using System.Text; 
using System.Collections; 
using System.Web; 
using System.Drawing;

namespace CWD
{
    #region RTF工具
    public class RTFUtil
    {
        public static string RTFEncodeString(string inStr)
        {            
            string Result = string.Empty;
            byte[] gbk = Encoding.GetEncoding("GBK").GetBytes(inStr);

            foreach (byte b in gbk)
            {
                Result += "\\'" + String.Format("{0:x2}", b);
            }           
            return Result;
        }
        public static string RTFEncodeColor(Color c)
        {
            byte r = c.R;
            byte g = c.G;
            byte b = c.B;
            return "\\red" + r.ToString() + "\\green" + g.ToString() + "\\blue" + r.ToString() + ";";
        }
    }
    #endregion
    #region RTF颜色表
    public class RTFColorTable : System.Collections.CollectionBase
    {
        public RTFColorTable()
        {
            List.Add(Color.Black);
            List.Add(Color.White);
        }
        public void Add(Color c)
        {
            if (!List.Contains(c))
                List.Add(c);
        }
        public void Delete(Color c)
        {
            List.Remove(c);
        }
        public void Delete(int index)
        {
            List.RemoveAt(index);
        }
        public bool Contains(Color c)
        {
            return List.Contains(c);
        }
        public int IndexOf(Color c)
        {
            return List.IndexOf(c);
        }
        public string RTFSource
        {
            get
            {
                string r = "{\\colortbl ;";
                for (int iCnt = 0; iCnt < List.Count; iCnt++)
                {
                    r += RTFUtil.RTFEncodeColor((Color)List[iCnt]);
                }
                r += "}";
                return r;
            }
        }
    }
    #endregion
    #region RTF字体表
    public class RTFontTable : System.Collections.CollectionBase
    {
        public RTFontTable()
        {
            List.Add("宋体");
            List.Add("FixedSys");
            List.Add("Cordia New");
        }
        public void Add(string fName)
        {
            List.Add(fName);
        }
        public void Delete(string fName)
        {
            List.Remove(fName);
        }
        public bool Contains(string fName)
        {
            return List.Contains(fName);
        }
        public void Delete(int index)
        {
            List.RemoveAt(index);
        }
        public int IndexOf(string f)
        {
            return List.IndexOf(f);
        }
        public string RTFSource
        {
            get
            {
                string r = "{\\fonttbl";
                for (int iCnt = 0; iCnt < List.Count; iCnt++)
                {
                    r += "{\\f" + iCnt.ToString() + "\\fnil\\fprq6\\fcharset134 ";
                    r += RTFUtil.RTFEncodeString((string)List[iCnt]) + ";}";
                }
                r += "}";
                return r;
            }
        }
    }
    #endregion

    #region RTF文档类
    public class RTFDocument
    {
        private string RTFHeader = "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052";
        private string RTFGenerator = "{\\*\\generator eglic.com;}";
        private RTFColorTable Colors;
        private RTFontTable Fonts;
        private string RTFContent = "";
        public RTFDocument()
        {
            Colors = new RTFColorTable();
            Fonts = new RTFontTable();
        }
        public void AppendText(string sText, int iSize, Color cColor, string sFont,
        bool bBlod, bool bItalic, bool bUnderLine, bool bNewLine)
        {
            if (!Colors.Contains(cColor))
                Colors.Add(cColor);
            if (!Fonts.Contains(sFont))
                Fonts.Add(sFont);
            string tmp = RTFUtil.RTFEncodeString(sText);           
            
            if (bBlod) tmp = "\\b" + tmp + "\\b0";
            if (bUnderLine) tmp = "\\ul" + tmp + "\\ulnone";
            if (bItalic) tmp = "\\i" + tmp + "\\i0";
            int ci = Colors.IndexOf(cColor); if (ci < 0) ci = 0; ci++;
            int fi = Fonts.IndexOf(sFont); if (fi < 0) fi = 0;
            tmp = "\\fs" + iSize.ToString() + tmp + "\\fs10";
            tmp = "\\f" + fi.ToString() + tmp + "\\f0";
            tmp = "\\cf" + ci.ToString() + tmp + "\\cf0";
            if (bNewLine) tmp += "\\par";
            RTFContent += tmp;
        }
        public string RTFSource
        {
            get
            {
                return RTFHeader + this.Fonts.RTFSource + this.Colors.RTFSource + this.RTFGenerator + @"\viewkind4" + RTFContent + "}";
            }
        }
    }
    #endregion 

}